<?php

require_once 'recaptchalib.php';

function smarty_function_form($params)
{
	global $smarty;
	
	$id = $params['id'];
	
	$form = query2array("
		SELECT id, code, name, action, descr, captcha_key_public
		FROM cf_forms
		WHERE id='$id'
	");
	$smarty->assign('form', $form);
	
	$fields = query2arrays("
		SELECT name, code, tp, mandatory, val, fmt
		FROM cf_fileds_in_forms
		INNER JOIN cf_form_fields ON cf_fileds_in_forms.field_id = cf_form_fields.id
		WHERE form_id={$form['id']}
		ORDER BY sort_order
	");
	
	if (array_key_exists('values',$params))
	{
		$values = $params['values'];
		for ($i=0; $i<count($fields); ++$i)
		{
			$f = $fields[$i];
			if (array_key_exists($f['code'], $values))
			{
				$fields[$i]['val'] = $values[ $f['code'] ];
			}
		}
	}
	
	$smarty->assign('fields', $fields);
	
	if (!empty($form['captcha_key_public']))
	{
	
		$smarty->assign('captcha',recaptcha_get_html($form['captcha_key_public']));
	}
	
	return $smarty->display('cf/templates/form.tpl');
}

?>